package Demo;

public class ArrayUtil {
	
	//创建数组工具类
	//求最大值
	public static int getMax(int[] arr){
		
		int max = arr[0];
		for (int i = 0; i < arr.length; i++) {
			if(max < arr[i])
				max = arr[i];
		}
		return max;
		
	}
	//求最小值
	public static int getMin(int[] arr){
		int min = arr[0];
		for (int i = 0; i < arr.length; i++) {
			if(min > arr[i])
				min = arr[i];
		}
		return min;
		
	}
	
	//求和
	public static int getSum(int[] arr){
		int sum = 0;
		for (int i = 0; i < arr.length; i++) {
			sum += arr[i];
		}
		return sum;
	}
	
	//求平均值
	public static int getAverage(int[] arr){
		
		return getSum(arr)/arr.length;
	}
	
	//反转
	public static void getReversal(int[] arr){
		
		for (int i = 0; i < arr.length/2; i++) {
			int temp = arr[i];
			arr[i] = arr[arr.length - 1 - i];
			arr[arr.length - 1 - i] = temp;
		}
		getAll(arr);
	}
	
	//复制
	public static int[] copy(int[] arr){
		int[] arr1 = new int[arr.length];
		for(int i = 0; i < arr.length; i++){
			arr1[i] = arr[i];
		}
		return arr1;
	}
	//线性查询
	public static boolean search(int[] arr,int num){
		
		for(int i = 0; i < arr.length; i++){
			if(num == arr[i])
				return true;
		}
		return false;
		
	}
	
	//冒泡排序
	public static void sort(int[] arr){
		//两两比较轮次 arr.length - 1
		for(int i = 0; i < arr.length - 1; i++){
			//每轮次的比较次数 arr.length - 1 - i
			for(int j = 0; j < arr.length - 1 - i; j++){
				if(arr[j] > arr[j + 1]){
					int temp = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1 ] = temp;
				}
				
			}
		}
		getAll(arr);
		
	}
	
	//遍历
	public static void getAll(int[] arr){
		for(int i = 0; i < arr.length; i++){
			System.out.print(arr[i] + " " );
		}
	}
	
	public static void getAll(int[][] arr){
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j] + " ");
			}
			System.out.println();
		}
	}
	
	//二分法查找
	public static boolean binarySearch(int[] arr,int num){
		//第一步 设置起始和终点
		int min = 0,max = arr.length - 1;
		
		//采用while循环遍历
		while(min <= max){
			int mid = (min + max) / 2;
			if(arr[mid] == num){
				return true;
			}else if(arr[mid] > num){
				max = mid - 1;
			}else if(arr[mid] < num)
				min = mid +1;
		}
		
		return false;
	}
	
	//杨辉三角形
	public static void yangHui(int[][] arr){
		for(int i = 0; i < arr.length; i++){
			arr[i] = new int[i + 1];
			for(int j = 0; j < arr[i].length; j++){
				if(j == 0){
					arr[i][j] = 1;
				}else if(j == arr[i].length - 1){
					arr[i][j] = 1;
				}else{
					arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j]; 
				}
				//System.out.print(arr[i][j] + "\t");
			}
			//System.out.println();
		}
		getAll(arr);
		
	}
	
}
